﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using PsychSurvey.Properties;

namespace PsychSurvey.Classes
{
    public class StudyFileHandler
    {
        HttpContext _context;
        string _appRoot;
        string _studyId;
        public StudyFileHandler(HttpContext Context, string StudyId)
        {
            _context = Context;
            _studyId = StudyId;
            //Check to see if the folder exists
            if (Directory.Exists(GetRootFolderForStudy()) == false)
            {
                Directory.CreateDirectory(GetRootFolderForStudy());
            }
        }
        public StudyFileHandler(string AppRoot,string StudyId)
        {
            _context = null;
            _appRoot = AppRoot;
            _studyId = StudyId;
            //Check to see if the folder exists
            if (Directory.Exists(GetRootFolderForStudy()) == false)
            {
                Directory.CreateDirectory(GetRootFolderForStudy());
            }
        }
        public string GetRootFolderForStudy()
        {
            string FolderName = _studyId.Replace("-", "_");
            string FullPath;
            if (_context != null)
            {
                FullPath = _context.Server.MapPath("~/Upload/" + FolderName);
            }
            else
            {
                FullPath = _appRoot + "/Upload/" + FolderName;
            }
            return FullPath;
        }
        public void UploadFileToStudyFolder(string FileName,Stream FileStream)
        {            
            //Check to see if that filename already exists
            string FullPath = GetRootFolderForStudy() + "/" + FileName;
            if (File.Exists(FullPath) == false)
            {
                using (FileStream fs = File.Create(FullPath))
                {
                    SaveFile(FileStream, fs);
                }
            }
        }
        public List<string> GetContentsForStudy()
        {
            string[] FileNames =  FileSystemUtil.GetAllFileNames(GetRootFolderForStudy());
            List<string> VirtualNames = new List<string>();
            foreach (string File in FileNames)
            {
                VirtualNames.Add(MapURL(File));
            }
            return VirtualNames;
        }
        public void DeleteFile(string Path)
        {
            if (_context != null)
            {
                File.Delete(_context.Server.MapPath(Path));
            }
            else
            {
                //File.Delete(MapURL(Path));
                throw new ApplicationException("Not written yet.");
            }
        }
        private void SaveFile(Stream stream, FileStream fs)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {
                fs.Write(buffer, 0, bytesRead);
            }
        }
        private string MapURL(string pathToMap)
        {
            string url;
            if (_context != null)
            {
                string AppPath = _context.Server.MapPath("~");
#if (DEBUG)
                url = String.Format(Settings.Default.ApplicationRoot_local + "{0}", pathToMap.Replace(AppPath, "".Replace("\\", "/")));                
#else
                url = String.Format(Settings.Default.ApplicationRoot + "{0}", pathToMap.Replace(AppPath, "".Replace("\\", "/")));                
#endif

            }
            else
            {                
#if (DEBUG)
                url = String.Format(Settings.Default.ApplicationRoot_local + "{0}", pathToMap.Replace(_appRoot, "".Replace("\\", "/")));
#else
                url = String.Format(Settings.Default.ApplicationRoot + "{0}", pathToMap.Replace(_appRoot, "".Replace("\\", "/")));                
#endif
            }
            return url;
        }
    }
}
